interface Commit {
  sha: string
  commit: {
    message: string;
    author: {
      name: string
    };
    committer: {
      date: string
    }
  }
}

export async function getGiteeCommits(ownerRepo: string, token?: string): Promise<Commit[]> {
  try {
    const headers: HeadersInit = {
      'Content-Type': 'application/json'
    }

    // 如果有token，添加到请求头
    if (token) {
      headers['Authorization'] = `token ${token}`
    }

    const response = await fetch(
      `https://gitee.com/api/v5/repos/${ownerRepo}/commits?per_page=100&page=1`,
      { headers }
    )

    if (!response.ok) {
      throw new Error(`Gitee API错误: ${response.status} ${response.statusText}`)
    }

    const commits = await response.json()
    return commits
  } catch (error) {
    console.error('获取Gitee提交记录失败:', error)
    throw error
  }
}
